package foodtable;

import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.List;

import com.andro.chansung.ConstantId;
import com.andro.chansung.ConstantName;
import com.andro.chansung.DB;

import net.htmlparser.jericho.Element;
import net.htmlparser.jericho.HTMLElementName;
import net.htmlparser.jericho.Source;

import android.app.Activity;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.os.Handler;
import android.util.Log;

public class IndukInjungFoodUpdateThread extends Thread {
	private String url;
	private Activity activity;
	private Handler handler;
	private String tableName;
	
	public IndukInjungFoodUpdateThread(Activity activity, Handler handler, String tableName, String url) {
		this.activity = activity;
		this.handler = handler;
		this.url = url;
		this.tableName = tableName;
	}
	
	public void run() {
		try {
			Source source = new Source(new URL(url));
			source.fullSequentialParse();
			
			Element table 			= source.getAllElements(HTMLElementName.TABLE).get(3);
			Element morningTrTag	= table.getChildElements().get(0);
			Element dinnerTrTag 	= table.getChildElements().get(1);
			
			List<Element> morningMenuList = morningTrTag.getChildElements().subList(1, morningTrTag.getChildElements().size());
			List<Element> dinnerMenuList  = dinnerTrTag.getChildElements().subList(1, dinnerTrTag.getChildElements().size());
			

			SQLiteDatabase db = activity.openOrCreateDatabase(ConstantName.DATABASE_NAME, Context.MODE_WORLD_WRITEABLE, null);
			
			// 0 -> monday
			Element tdTag		= morningMenuList.get(0);
			DB.insertDataIntoIndukOrInjungFoodTable(db, tableName,
											new Food(ConstantId.FOOD_KIND.A, 
													 ConstantId.FOOD_TIME.BREAKFAST, 
													 ConstantId.FOOD_DAY.MONDAY, 
													 tdTag.getContent().getRenderer().toString()));
			// 1 -> tuesday
			tdTag	= morningMenuList.get(1);
			DB.insertDataIntoIndukOrInjungFoodTable(db, tableName, 
											new Food(ConstantId.FOOD_KIND.A, 
													 ConstantId.FOOD_TIME.BREAKFAST, 
													 ConstantId.FOOD_DAY.TUESDAY, 
													 tdTag.getContent().getRenderer().toString()));
			// 2 -> wednesday
			tdTag	= morningMenuList.get(2);
			DB.insertDataIntoIndukOrInjungFoodTable(db, tableName,
											new Food(ConstantId.FOOD_KIND.A, 
													 ConstantId.FOOD_TIME.BREAKFAST, 
													 ConstantId.FOOD_DAY.WEDNESDAY, 
													 tdTag.getContent().getRenderer().toString()));
			// 3 -> thursday
			tdTag	= morningMenuList.get(3);
			DB.insertDataIntoIndukOrInjungFoodTable(db, tableName,
											new Food(ConstantId.FOOD_KIND.A, 
													 ConstantId.FOOD_TIME.BREAKFAST, 
													 ConstantId.FOOD_DAY.THURSDAY, 
													 tdTag.getContent().getRenderer().toString()));
			// 4 -> friday
			tdTag		= morningMenuList.get(4);
			DB.insertDataIntoIndukOrInjungFoodTable(db, tableName,
											new Food(ConstantId.FOOD_KIND.A, 
													 ConstantId.FOOD_TIME.BREAKFAST, 
													 ConstantId.FOOD_DAY.FRIDAY, 
													 tdTag.getContent().getRenderer().toString()));
			// 5 -> saturday
			tdTag	= morningMenuList.get(5);
			DB.insertDataIntoIndukOrInjungFoodTable(db, tableName,
											new Food(ConstantId.FOOD_KIND.A, 
													 ConstantId.FOOD_TIME.BREAKFAST, 
													 ConstantId.FOOD_DAY.SATURDAY, 
													 tdTag.getContent().getRenderer().toString()));
			// 6 -> sunday
			tdTag		= morningMenuList.get(6);
			DB.insertDataIntoIndukOrInjungFoodTable(db, tableName,
											new Food(ConstantId.FOOD_KIND.A, 
													 ConstantId.FOOD_TIME.BREAKFAST, 
													 ConstantId.FOOD_DAY.SUNDAY, 
													 tdTag.getContent().getRenderer().toString()));
			
			///////////////////////////////////////////////////////////////////////////////////
			// 0 -> monday
			tdTag		= dinnerMenuList.get(0);
			DB.insertDataIntoIndukOrInjungFoodTable(db, tableName,
											new Food(ConstantId.FOOD_KIND.A, 
													 ConstantId.FOOD_TIME.DINNER, 
													 ConstantId.FOOD_DAY.MONDAY, 
													 tdTag.getContent().getRenderer().toString()));
			// 1 -> tuesday
			tdTag	= dinnerMenuList.get(1);
			DB.insertDataIntoIndukOrInjungFoodTable(db, tableName,
											new Food(ConstantId.FOOD_KIND.A, 
													 ConstantId.FOOD_TIME.DINNER, 
													 ConstantId.FOOD_DAY.TUESDAY, 
													 tdTag.getContent().getRenderer().toString()));
			// 2 -> wednesday
			tdTag	= dinnerMenuList.get(2);
			DB.insertDataIntoIndukOrInjungFoodTable(db, tableName,
											new Food(ConstantId.FOOD_KIND.A, 
													 ConstantId.FOOD_TIME.DINNER, 
													 ConstantId.FOOD_DAY.WEDNESDAY, 
													 tdTag.getContent().getRenderer().toString()));
			// 3 -> thursday
			tdTag	= dinnerMenuList.get(3);
			DB.insertDataIntoIndukOrInjungFoodTable(db, tableName,
											new Food(ConstantId.FOOD_KIND.A, 
													 ConstantId.FOOD_TIME.DINNER, 
													 ConstantId.FOOD_DAY.THURSDAY, 
													 tdTag.getContent().getRenderer().toString()));
			// 4 -> friday
			tdTag	= dinnerMenuList.get(4);
			DB.insertDataIntoIndukOrInjungFoodTable(db, tableName,
											new Food(ConstantId.FOOD_KIND.A, 
													 ConstantId.FOOD_TIME.DINNER, 
													 ConstantId.FOOD_DAY.FRIDAY, 
													 tdTag.getContent().getRenderer().toString()));
			// 5 -> saturday
			tdTag	= dinnerMenuList.get(5);
			DB.insertDataIntoIndukOrInjungFoodTable(db, tableName,
											new Food(ConstantId.FOOD_KIND.A, 
													 ConstantId.FOOD_TIME.DINNER, 
													 ConstantId.FOOD_DAY.SATURDAY, 
													 tdTag.getContent().getRenderer().toString()));
			// 6 -> sunday
			tdTag	= dinnerMenuList.get(6);
			DB.insertDataIntoIndukOrInjungFoodTable(db, tableName,
											new Food(ConstantId.FOOD_KIND.A, 
													 ConstantId.FOOD_TIME.DINNER, 
													 ConstantId.FOOD_DAY.SUNDAY, 
													 tdTag.getContent().getRenderer().toString()));
			
			db.close();
			source.clearCache();
			handler.sendEmptyMessage(0);
		} catch (MalformedURLException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}
